package com.liuliang.http;

import java.util.HashMap;

/**
 * 返回结果的封装类，继承HashMap
 */
public class AjaxResult extends HashMap<String, Object> {
    //定义HashMap的key（状态码，状态描述，数据，总记录数）
    private static final String STATUS = "status";
    private static final String MESSAGE = "message";
    private static final String DATA = "data";
    private static final String TOTAL = "total";

    //引入枚举状态类
    private EnumStatus enumStatus;

    //构造方法私有化，不让别人去new
    public AjaxResult(EnumStatus enumStatus) {
        this.put(STATUS, enumStatus.getStatus());
        this.put(MESSAGE, enumStatus.getMessage());
        this.enumStatus = enumStatus;
    }

    //创建AjaxResult对象
    private static AjaxResult ajaxResult(EnumStatus enumStatus) {
        return new AjaxResult(enumStatus);
    }

    //默认返回成功的状态码和信息
    public static AjaxResult success() {
        return ajaxResult(EnumStatus.SUCCESS);
    }

    //返回成功后携带数据
    public static AjaxResult success(Object data) {
        AjaxResult success = success();
        success.put(DATA, data);
        return success;
    }

    //返回成功后携带数据和总记录数
    public static AjaxResult success(Object data, Long total) {
        AjaxResult success = success();
        success.put(DATA, data);
        success.put(TOTAL, total);
        return success;
    }
}